package com.example.demo.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.sys.entity.Classdiscuss;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.controller.Result;
import com.example.demo.sys.mapper.CoursevideoMapper;
import com.example.demo.sys.entity.Coursevideo;

import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.tools.BaseController;

/**
 * <p>
    *  前端控制器 chrimer
    * </p>
 *
 * @author chrimer
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/sys/coursevideo")
public class CoursevideoController extends BaseController {
    @Autowired
    private CoursevideoMapper coursevideoMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        String id) {
        Result result = new Result();
        Coursevideo parms = new Coursevideo();
        Page<Coursevideo> page = new Page<Coursevideo>(1, 100);
        QueryWrapper<Coursevideo> warpper = new QueryWrapper<Coursevideo>(parms);
        warpper.eq("courseId",id);
        result.setData(coursevideoMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Coursevideo entity) {
        Result result = new Result();
        Coursevideo coursevideo = new Coursevideo();
        coursevideo.setName(entity.getName());
        coursevideo.setCourseId(entity.getCourseId());
        Coursevideo coursevideoExit = coursevideoMapper.selectOne(new QueryWrapper<Coursevideo>(coursevideo));
        //System.out.println(coursevideoExit);
        if(coursevideoExit != null)
        {
            coursevideoExit.setVideoUrl(entity.getVideoUrl());
            coursevideoMapper.updateById(coursevideoExit);
            result.success("该节点已经存在，内容成功替换");
        }
        else if (entity.getId() == null||"".equals(entity.getId())) {
            coursevideoMapper.insert(entity);
            result.success("节点设置成功");
        } else {
                coursevideoMapper.updateById(entity);
        }
        return result;
    }


    //查看课程视频信息树
    @RequestMapping(method= RequestMethod.POST,value="/select")
    public Result select(String id) {
        Result result = new Result();
        Coursevideo parms = new Coursevideo();
        QueryWrapper<Coursevideo> warpper = new QueryWrapper<Coursevideo>(parms);
        List< Coursevideo> videoInfor = null;
        warpper.eq("courseId",id);
        warpper.orderByAsc("name");
        videoInfor = coursevideoMapper.selectList(warpper);
        Map<String, Object> map = new HashMap<>();
        map.put("videoInfor", videoInfor);
        result.setData(map);
        return result;
    }

    //查找视频树的已有父节点
    @RequestMapping(method= RequestMethod.POST,value="/selectFather")
    public Result selectFather(String courseId) {
        Result result = new Result();
        Coursevideo parms = new Coursevideo();
        QueryWrapper<Coursevideo> warpper = new QueryWrapper<Coursevideo>(parms);
        List< Coursevideo> treeFather = null;
        warpper.nested(i-> i.eq("childName","0").eq("courseId",courseId));
        treeFather = coursevideoMapper.selectList(warpper);
        Map<String, Object> map = new HashMap<>();
        map.put("treeFather", treeFather);
        result.setData(map);
        return result;
    }

    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
    coursevideoMapper.deleteBatchIds(deleteIds);
        return result;
    }

}
